#! /usr/bin/env python3

import sys # open, read, write
import os # mkdir
import shutil

filedict = {}

def append(file, line):
    if file not in filedict: # 判断key是否在字典
        fp = open(file, 'w')
        filedict.update({file: fp}) # 追加到字典
    fp = filedict[file] # 字典访问
    fp.write(line)


if __name__ == '__main__':
    if len(sys.argv) != 2:
        print('Please input a fwlog!')
        exit(1)

    inputs = sys.argv[1]
    output = 'output'

    if os.path.exists(output):
        shutil.rmtree(output)
    os.mkdir(output)

    with open(inputs, "r") as f:
        lines = f.readlines()
        for line in lines:
            if 'fw_binary' in line:
                continue
            word = line.split(' ')
            session = word[2]
            append(output + '/' + session + '.log', line)
